<!-- menu表单弹窗 -->
<form id="foodMenuForm" lay-filter="foodMenuForm" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <select name="type" lay-verify="required" lay-filter="foodMenuForm-type">
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input name="name" type="text" class="layui-input" maxlength="255" lay-verify="required" />
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input name="price" type="text" class="layui-input" maxlength="11" lay-verify="number"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea name="introduce" type="text" class="layui-textarea" maxlength="255" lay-verify="required"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input name="status" value="1" type="checkbox" lay-skin="switch" lay-text="售中|售完" lay-filter="foodMenuForm-status"/>
        </div>
    </div>

    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="foodMenuForm-submit" type="button" lay-submit>保存</button>
    </div>

</form>

<script>
    layui.use(['layer', 'form', 'admin','http','tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var http = layui.http;
        var tools = layui.tools;
        var param = tools.param;

        var $view = $("#foodMenuForm");
        var $type = $view.find("select[name='type']");
        var $status = $view.find("input[name='status']");

        if(param.data){
            form.val('foodMenuForm',param.data);
        }
        if(param.foodTypeList){
            tools.select.options($type,param.foodTypeList,param.data.type);
        }

        form.render('select','foodMenuForm');
        form.render('checkbox','foodMenuForm');
        // 表单提交事件
        form.on('submit(foodMenuForm-submit)', function (data) {
            var rowData = param.data;
            data.field.status = $status.val();
            http.post('foodMenu/save', data.field, function (res) {
                layer.closeAll('loading');
                if (res.ok) {
                    //修改
                    if(rowData.id && param.rowObj){  //更新
                        param.rowObj.update(res.data);
                        //param.tableIns.updateRow(param.rowIndex,res.data,false,true);
                        param.tableIns.renderForm();
                    }else{ //新增
                        //param.fnSearch();
                        param.tableIns.addRow(res.data,null,true);
                    }
                    //http.okMsg(res.msg);
                    admin.closeDialog('#foodMenuForm');
                } else {
                    http.fail(res);
                }
            });
            return false;
        });
        form.on('switch(foodMenuForm-status)',function(data){
            $status.val(data.elem.checked ? 1:0);
        });
    });
</script>